import com.adobe.cairngorm.control.CairngormEventDispatcher;

import flash.events.Event;
import flash.events.MouseEvent;

import mx.controls.Alert;

import net.optix.ip.events.GetValueEvent;
import net.optix.ip.events.SetValueEvent;
import net.optix.ip.model.ModelLocator;
import net.optix.ip.vo.ValueVO;

private function initApp():void
{
	var getValue:GetValueEvent	=	new GetValueEvent(model.currentInvention.inventionID);
	com.adobe.cairngorm.control.CairngormEventDispatcher.getInstance().dispatchEvent(getValue);	
}

private function showMoney(event:Event):void
{
	event.target.text	=	purseMoney(getMoney(event.target.text));
}

private function getMoney(money:String):String
{
	var str:String		=	"";
	for(var i:int=0; i<money.length; i++)
	{
		if(money.charAt(i) != "," && money.charAt(i) != " ")
		{
			str	=	str+money.charAt(i);
		}
	}
	return str;
}

private function purseMoney(money:String):String
{
	var arr:Array	=	money.split(".");
	if(arr.length == 1)
	{
		return purseMoneyBefore(money);
	}
	else if(arr.length == 2)
	{
		var str1:String;
		str1	=	purseMoneyBefore(arr[0])+" . "+purseMoneyAfter(arr[1]);
		return str1;
	}
	return "";
}

private function purseMoneyBefore(money:String):String
{
	var str:String	=	money.charAt(0);
	var length:int	=	money.length;
		
	for(var i:int=1; i<money.length; i++)
	{
		if(((length-i)/3).toString().length == 1)
		{
			str	=	str+" , "+money.charAt(i);
		}
		else
		{
				
			str	=	str+money.charAt(i);
		}
	}
	return str;
}

private function purseMoneyAfter(money:String):String
{
	var str:String	=	money.charAt(0);
	var length:int	=	money.length;
		
	for(var i:int=1; i<money.length; i++)
	{
		if((i/3).toString().length == 1)
		{
			str	=	str+" , "+money.charAt(i);
		}
		else
		{
				
			str	=	str+money.charAt(i);
		}
	}
	return str;
}

[Bindable]
private var model:ModelLocator	=	ModelLocator.getInstance();		
	
			
			private	var cpat1:Number	=	0.3;
			private	var	cpat2:Number	=	0.35;
			private	var cpat3:Number	=	0.5;
			private	var	shdval:Number	=	25000;
			private	var	cryl1:Number	=	0.07;
			private	var	cryl2:Number	=	0.08;
			private	var	rylcst:Number	=	0.04;
			private var revcst:Number	=	0.16;
			
			private	var opp:Number;
			private	var units:Number;
			private	var ta:Number;
			private	var	patentablity:Number;
			private	var shield:Number;
			private var value:Number;
			
private	function computer():void
{
	opp	=	int(getMoney(this.tam.text))*int(this.sam.text)/100
			*int(this.impact.text)/100/(1-int(this.grow.text)/100) - Number(getMoney(this.devcost.text));
	
	opp	=	opp<0?-opp>>0:opp>>0;
					
	units=	int(opp/int(getMoney(asp.text)));
				
	ta	=	(Number(getMoney(this.ca.text))+Number(getMoney(this.pa.text)))*this.units;
				
	patentablity	=	this.cpat3*Math.sqrt(1-(this.samepb.selectedValue)/5)
					+ this.cpat1*(this.pat1pb.selectedValue)/5*(this.pat1pb.selectedValue)/5
					+this.cpat2*(this.pat2pb.selectedValue)/5*(this.pat2pb.selectedValue)/5;
					
	shield	=		cryl1*((blk1pb.selectedValue)/5)*((blk1pb.selectedValue)/5)*opp*rylcst+
					cryl2*this.shdval*((blk2pb.selectedValue)/5)*((blk2pb.selectedValue)/5);
	
	value	=	int((ta+shield)*patentablity);
	
	auctionValue.text	=	value.toString();
	
	var title:String	=	"Result of valuation ";
	var message:String	=	"";
				
	message	=	message+"Opporunity is $"+int(opp)+"\n";
	message	=	message+"Units is "+int(this.units)+"\n";
	message	=	message+"Total Advantange is $"+int(this.ta)+"\n";
	message	=	message+"Patentability is "+this.patentablity+" .\n";
	
	message	=	"The invention valuation is "+value+"\n";
	message	+=	"OPP = "+this.opp+"\n";
	message	+=	"UNIT = "+this.units+"\n";
	message	+=	"PATA = "+this.patentablity+"\n";
	message	+=	"and SHLD = "+this.shield+"\n";
				
	var alert:Alert	=	 
			Alert.show(message,title,Alert.OK, null,null, null,  Alert.OK );
	alert.width		=	300;
	alert.height	=	200;
	
	var vo:ValueVO	=	new ValueVO();
	try
	{
		vo.inventionID		=	model.currentInvention.inventionID;
		vo.auction			=	value;
				
		vo.area				=	area.selectedIndex;
		vo.definition		=	definition.text; 
		vo.rev				=	Number(getMoney(rev.text));
		
		vo.tam				=	Number(getMoney(tam.text));		
		vo.sam				=	int(sam.text);
		vo.impact			=	int(impact.text);
		vo.grow				=	int(grow.text);
				
		
		vo.asp				=	Number(getMoney(asp.text));
		vo.ca				=	Number(getMoney(ca.text));
		vo.pa				=	Number(getMoney(pa.text));
		vo.devcost			=	Number(getMoney(devcost.text));
		
		vo.altad			=	Number(getMoney(altad.text));
				
		vo.funcad			=	funcad.selectedValue;
		vo.samepb			=	samepb.selectedValue;
		vo.pat1pb			=	pat1pb.selectedValue;
		vo.pat2pb			=	pat2pb.selectedValue;
		vo.blk1pb			=	blk1pb.selectedValue;
		vo.blk2pb			=	blk2pb.selectedValue;
		
		
		vo.funcad			=	funcad.selectedValue;
		vo.samepb			=	samepb.selectedValue;
		vo.pat1pb			=	pat1pb.selectedValue;
		vo.pat2pb			=	pat2pb.selectedValue;
		vo.blk1pb			=	blk1pb.selectedValue;
		vo.blk2pb			=	blk2pb.selectedValue;
	}
	catch(error:Error){}
	var event:SetValueEvent	=	new SetValueEvent(vo);
	com.adobe.cairngorm.control.CairngormEventDispatcher.getInstance().dispatchEvent(event);
}

private function changeStatus(event:MouseEvent):void
{
	
}